﻿using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace TemplateAddIn
{
    public class TextVariable : Variable
    {
        #region Implemented Abstract Methods
        public override bool SetContent(object pContent)
        {
            bool raiseEvent = false;
            string tempContent = (string)pContent;

            if (((string)this.Content).ToLower() != tempContent.ToLower())
            {
                raiseEvent = true;
                this.content = pContent;
            }

            if (raiseEvent)
                this.RaiseContentChanged();

            return raiseEvent;
        }
        #endregion

        protected override void Variable_ContentChanged(Variable sender)
        {
            List<Word.ContentControl> removeList=new List<Microsoft.Office.Interop.Word.ContentControl>();

            foreach (Word.ContentControl cc in this.ContentControlList)
            {
                try
                {
                    cc.Range.Text = (string)this.content;
                }
                catch (Exception ex)
                {
                    removeList.Add(cc);
                }
            }

            foreach (Word.ContentControl cc in removeList)
            {
                this.ContentControlList.Remove(cc);
            }
        }

        public TextVariable(string pName) : base(pName, Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText) { }

        public TextVariable(string pName, string pContent)
            : base(pName, Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
        {
            this.content = pContent;
        }

        public override string FullName
        {
            get { return "Var(Text): " + this.Name; }
        }
    }
}
